"""
AI角色扮演API包
提供角色扮演和语音对话的API接口
"""

# 核心模型导入
from api.models.llm_client import LLMClient
from api.models.knowledge_base import KnowledgeBase

# 角色系统导入  
from api.agent.role_chain_agent import RoleChainAgent

# API路由导入
from api.role_play_api import router as role_play_router

# FastAPI应用导入
from api.main import app

# 便捷接口
class APIClient:
    """API客户端，简化API调用"""
    
    def __init__(self, base_url: str = "http://localhost:8000"):
        """初始化API客户端
        
        Args:
            base_url: API基础URL
        """
        self.base_url = base_url
        self.role_play_base = f"{base_url}/role-play"
        
    def get_roles(self):
        """获取所有角色"""
        import requests
        response = requests.get(f"{self.role_play_base}/roles")
        return response.json() if response.status_code == 200 else []
        
    def select_role(self, role_id: str):
        """选择角色"""
        import requests
        response = requests.get(f"{self.role_play_base}/select/{role_id}")
        return response.json() if response.status_code == 200 else None
        
    def chat(self, message: str, role_id: str = None, session_id: str = None):
        """与角色对话"""
        import requests
        data = {"message": message}
        if role_id:
            data["role_id"] = role_id
        if session_id:
            data["session_id"] = session_id
            
        response = requests.post(f"{self.role_play_base}/chat", json=data)
        return response.json() if response.status_code == 200 else {"success": False}
        
    def chat_stream(self, message: str, role_id: str = None, session_id: str = None):
        """与角色进行流式对话"""
        import requests
        import json
        
        data = {"message": message}
        if role_id:
            data["role_id"] = role_id
        if session_id:
            data["session_id"] = session_id
            
        headers = {'Accept': 'text/event-stream'}
        response = requests.post(
            f"{self.role_play_base}/chat/stream",
            json=data,
            headers=headers,
            stream=True
        )
        
        if response.status_code == 200:
            import sseclient
            client = sseclient.SSEClient(response)
            for event in client.events():
                yield json.loads(event.data)

# 导出所有接口
__all__ = [
    # 核心模型
    'LLMClient',
    'KnowledgeBase',
    
    # 角色系统
    'BaseRole',
    'AssistantRole',
    'TeacherRole', 
    'FriendRole',
    'RoleFactory',
    'RolePlayAgent',
    
    # API
    'role_play_router',
    'app',
    'APIClient',
]